package v1

import (
	"fmt"
	"io/ioutil"
	"usplatformserver/global"
	"usplatformserver/internal/model"
	"usplatformserver/internal/service"
	"usplatformserver/pkg/app"
	"usplatformserver/pkg/errcode"

	"github.com/gin-gonic/gin"
)

type Threshold struct{}

// 阈值获取结果
type thresholdInfo struct {
	Thresholds []model.ThresholdInfo
	Err        error
}

// LVEF获取结果
type lvefInfo struct {
	LVEF *model.ThresholdLVEFInfo
	Err  error
}

func NewThreshold() Threshold {
	return Threshold{}
}

// @Summer 获取检测阈值
// @Produce json
// @Param token header string true "token"
// @Success 200 {object} model.ThresholdListSwagger "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/threshold/det [get]
func (t *Threshold) GetThreshold(c *gin.Context) {
	getChan := make(chan thresholdInfo, 1)

	go func() {
		addr := fmt.Sprintf("tcp://%v:%d", global.ZMQSettings.Addr, global.ZMQSettings.Port)
		var ret thresholdInfo
		svc := service.New(c.Request.Context())
		thresholds, err := svc.GetThreshold(addr)
		ret.Err = err
		ret.Thresholds = thresholds
		getChan <- ret
	}()

	for {
		ret := <-getChan
		if ret.Err != nil {
			global.Logger.Infof("svc.GetThreshold failed: %v", ret.Err)
			response := app.NewResponse(c)
			response.ToErrorResponse(errcode.ErrorGetThresholdFail)
			break
		} else {
			response := app.NewResponse(c)
			response.ToResponse(gin.H{
				"list": ret.Thresholds,
			})
			break
		}
	}
	close(getChan)
}

// @Summer 设置检测阈值
// @Produce json
// @Param token header string true "token"
// @Param thresholds body model.ThresholdList true "检测阈值"
// @Success 200 {object} model.SwaggerSuccess "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/threshold/det [post]
func (t *Threshold) SetThreshold(c *gin.Context) {
	getChan := make(chan error, 1)

	//新建协程并执行
	go func() {
		addr := fmt.Sprintf("tcp://%v:%d", global.ZMQSettings.Addr, global.ZMQSettings.Port)

		reqBody, _ := ioutil.ReadAll(c.Request.Body)
		fmt.Println("SetThreshold reqBody: ", reqBody)
		svc := service.New(c.Request.Context())
		err := svc.SetThreshold(addr, string(reqBody))

		getChan <- err
	}()
	//获取协程数据
	for {
		err := <-getChan
		if err != nil {
			global.Logger.Infof("svc.SetThreshold failed: %v", err)
			response := app.NewResponse(c)
			response.ToErrorResponse(errcode.ErrorSetThresholdFail)
			break
		} else {
			response := app.NewResponse(c)
			response.ToResponseSuccess()
			break
		}
	}
	//关闭协程
	close(getChan)
}

// @Summer 获取LVEF自动保存阈值
// @Produce json
// @Param token header string true "token"
// @Success 200 {object} model.ThresholdLVEFInfoSwagger "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/threshold/lvef [get]
func (t *Threshold) GetLVEFThreshold(c *gin.Context) {
	getChan := make(chan lvefInfo, 1)

	//新建协程并执行
	go func() {
		addr := fmt.Sprintf("tcp://%v:%d", global.ZMQSettings.Addr, global.ZMQSettings.Port)
		var ret lvefInfo
		svc := service.New(c.Request.Context())
		lvef, err := svc.GetLVEFThreshold(addr)
		ret.Err = err
		ret.LVEF = lvef

		getChan <- ret
	}()
	//获取协程数据
	for {
		ret := <-getChan
		if ret.Err != nil {
			global.Logger.Infof("svc.GetLVEFThreshold failed: %v", ret.Err)
			response := app.NewResponse(c)
			response.ToErrorResponse(errcode.ErrorGetLVEFThresholdFail)
			break
		} else {
			response := app.NewResponse(c)
			response.ToResponse(ret.LVEF)
			break
		}
	}
	//关闭协程
	close(getChan)
}

// @Summer 设置LVEF阈值
// @Produce json
// @Param token header string true "token"
// @Param thresholds body model.ThresholdLVEFInfo true "lvef阈值"
// @Success 200 {object} model.SwaggerSuccess "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/threshold/lvef [post]
func (t *Threshold) SetLVEFThreshold(c *gin.Context) {
	getChan := make(chan error, 1)

	//新建协程并执行
	go func() {

		addr := fmt.Sprintf("tcp://%v:%d", global.ZMQSettings.Addr, global.ZMQSettings.Port)

		reqBody, _ := ioutil.ReadAll(c.Request.Body)
		fmt.Println("SetLVEFThreshold reqBody: ", reqBody)
		svc := service.New(c.Request.Context())
		err := svc.SetLVEFThreshold(addr, string(reqBody))

		getChan <- err
	}()
	//获取协程数据
	for {
		err := <-getChan
		if err != nil {
			global.Logger.Infof("svc.SetLVEFThreshold failed: %v", err)
			response := app.NewResponse(c)
			response.ToErrorResponse(errcode.ErrorSetLVEFThresholdFail)
			break
		} else {
			response := app.NewResponse(c)
			response.ToResponseSuccess()
			break
		}
	}
	//关闭协程
	close(getChan)
}
